package cn.edu.hncst.controller;

import cn.edu.hncst.entity.User;
import cn.edu.hncst.service.UserService;
import cn.edu.hncst.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        try {
            //获取前台传过来的map数据
            Map<String, String[]> map = req.getParameterMap();
            User user = new User();
            //将map数据封装成user对象
            BeanUtils.populate(user, map);
            UserService userService = new UserServiceImpl();
            //调用修改方法修改数据
            userService.updateUser(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        //重定向到
        resp.sendRedirect(req.getContextPath() + "/pageQueryServlet");
    }
}
